package cn.com.cifi.mars.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 
 * @Description：空数据处理
 * @author：yyd
 * @date：2020年1月7日 下午6:05:29
 */
public class EmptyUtils {

	private static final String separator = " / ";
	public static final BigDecimal NUM_10000 = new BigDecimal("10000");
	
	public static boolean isEmpty(final String s) {
		if (s == null) {
			return true;
		}
		return (s.isEmpty() || s.trim().length() == 0) || "null".equals(s);
	}

	public static boolean isNotEmpty(final String s) {
		return !isEmpty(s);
	}
	
	public static String handleNum(Integer num1,Integer num2) {
		return handleNumNullValue(num1 + separator + num2);
	}
	
	public static String handleNumNullValue(String strVal) {
		if(isEmpty(strVal)) {
			return "0";
		}
		if(strVal.indexOf("/") < 0) {
			if(isEmpty(strVal)) {
				strVal ="0";
			}
		}else {
			String t1 = strVal.substring(0, strVal.indexOf("/")).trim();
			String t2 = strVal.substring(strVal.indexOf("/")+1, strVal.length()).trim();
			if(isEmpty(t1)) {
				t1="0";
			}
			if(isEmpty(t2)) {
				t2="0";
			}
			return t1+separator+t2;
		}
		return strVal;
	}
	
	public static String handleAreaNullValue(BigDecimal area) {
		if(area == null) {
			return "0.00";
		}else {
			if(area.compareTo(BigDecimal.ZERO) == 0) {
				return "0.00";
			}
			return fmtPriceMicrometer(area,2);
		}
	}
	
	public static String handleAvg(BigDecimal housePrice,BigDecimal area,BigDecimal parkingPrice,Integer parkingNum) {
		BigDecimal houseAvg = StrUtils.divide(housePrice,area);
		BigDecimal parkingAvg = StrUtils.divide(parkingPrice,parkingNum==null?BigDecimal.ZERO:new BigDecimal(parkingNum));
		return handleAvgNullValue(fmtPriceMicrometer(houseAvg,0) + separator + fmtPriceMicrometer(parkingAvg,0));
	}
	
	public static String handleAvgNullValue(String strVal) {
		if(isEmpty(strVal)) {
			return "0";
		}
		if(strVal.indexOf("/") < 0) {
			if(isEmpty(strVal)) {
				return "0";
			}else {
				return fmtPriceMicrometer(strVal,0);
			}
		}else {
			String t1 = strVal.substring(0, strVal.indexOf("/")).trim();
			String t2 = strVal.substring(strVal.indexOf("/")+1, strVal.length()).trim();
			if(isEmpty(t1)) {
				t1="0";
			}else {
				t1=fmtPriceMicrometer(t1,0);
			}
			if(isEmpty(t2)) {
				t2="0";
			}else {
				t2=fmtPriceMicrometer(t2,0);
			}
			return t1+separator+t2;
		}
	}
	
	public static String handleAvgNullValue2(String strVal) {
		if(isEmpty(strVal)) {
			return "0";
		}
		if(strVal.indexOf("/") < 0) {
			if(isEmpty(strVal)) {
				return "0";
			}else {
				return strVal;
			}
		}else {
			String t1 = strVal.substring(0, strVal.indexOf("/")).trim();
			String t2 = strVal.substring(strVal.indexOf("/")+1, strVal.length()).trim();
			if(isEmpty(t1)) {
				t1="0";
			}
			if(isEmpty(t2)) {
				t2="0";
			}
			return t1+separator+t2;
		}
	}
	
	public static String handlePrice(BigDecimal housePrice,BigDecimal parkingPrice) {
		BigDecimal total = StrUtils.BigDecimalADD(housePrice,parkingPrice);
		return fmtToWanUnit(total)+separator+fmtToWanUnit(housePrice)+separator+fmtToWanUnit(parkingPrice);
	}
	
	public static String handlePriceNullValue(String strVal) {
		if(isEmpty(strVal)) {
			return "0";
		}
		if(strVal.indexOf("/") < 0) {
			if(isEmpty(strVal)) {
				return "0";
			}else {
				return fmtToWanUnit(strVal);
			}
		}else {
			String t1 = strVal.substring(0, strVal.indexOf("/")).trim();
			String t2 = strVal.substring(strVal.indexOf("/")+1, strVal.lastIndexOf("/")).trim();
			String t3 = strVal.substring(strVal.lastIndexOf("/")+1, strVal.length()).trim();
			if(isEmpty(t1)) {
				t1 = "0";	
			}else {
				t1 = fmtToWanUnit(t1);
			}
			
			if(isEmpty(t2)) {
				t2 = "0";	
			}else {
				t2 = fmtToWanUnit(t2);
			}
			
			if(isEmpty(t3)) {
				t3 = "0";	
			}else {
				t3 = fmtToWanUnit(t3);
			}
			return  t1+separator+t2+separator+t3;
		}
	}
	
	public static String handlePriceNullValue2(String strVal) {
		if(isEmpty(strVal)) {
			return "0";
		}
		if(strVal.indexOf("/") < 0) {
			if(isEmpty(strVal)) {
				return "0";
			}else {
				return strVal;
			}
		}else {
			String t1 = strVal.substring(0, strVal.indexOf("/")).trim();
			String t2 = strVal.substring(strVal.indexOf("/")+1, strVal.lastIndexOf("/")).trim();
			String t3 = strVal.substring(strVal.lastIndexOf("/")+1, strVal.length()).trim();
			if(isEmpty(t1)) {
				t1 = "0";	
			}
			
			if(isEmpty(t2)) {
				t2 = "0";	
			}
			
			if(isEmpty(t3)) {
				t3 = "0";	
			}
			return  t1+separator+t2+separator+t3;
		}
	}
	
	public static BigDecimal getPrice(String value) {
		if(isEmpty(value)) {
			return BigDecimal.ZERO;
		}
		if(value.indexOf("/") < 0) {
			if(isEmpty(value)) {
				return BigDecimal.ZERO;
			}else {
				return new BigDecimal(value);
			}
		}else {
			String t1 = value.substring(0, value.indexOf("/")).trim();
			if(isEmpty(t1)) {
				return BigDecimal.ZERO;
			}else {
				return new BigDecimal(t1);
			}
		}
	}
	
	public static String fmtToWanUnit(BigDecimal value) {
		if(value == null) {
			return "0";
		}
		BigDecimal vl = NumberUtils.divide(value,NUM_10000,0);
		return fmtPriceMicrometer(vl,0);
	}
	
	public static String fmtToWanUnit(String value) {
		String res = "";
		if (isEmpty(value)) {
			res = "0";
			return res;
		}
		if (value.indexOf(",") > 0) {
			value = value.replaceAll(",", "");
		}
		res = fmtPriceMicrometer(NumberUtils.divide(value, "10000", 0),0);
		return res;
	}
	
	public static String fmtPriceMicrometer(BigDecimal text,int scale) {
		DecimalFormat df = new DecimalFormat("###,##0.000");
		if(scale == 0) {
			df = new DecimalFormat("###,##0");
		}else if(scale == 1) {
			df = new DecimalFormat("###,##0.0");
		}else if(scale == 2) {
			df = new DecimalFormat("###,##0.00");
		}
		double number = 0.0;
		try {
			number = text.doubleValue();
		} catch (Exception e) {
			number = 0.0;
		}
		return df.format(number);
	}
	
	public static String fmtPriceMicrometer(String text,int scale) {
		if (text.indexOf(",") > 0) {
			text = text.replaceAll(",", "");
		}
		DecimalFormat df = new DecimalFormat("###,##0.000");
		if(scale == 0) {
			df = new DecimalFormat("###,##0");
		}else if(scale == 1) {
			df = new DecimalFormat("###,##0.0");
		}else if(scale == 2) {
			df = new DecimalFormat("###,##0.00");
		}
		double number = 0.0;
		try {
			number = Double.parseDouble(text);
		} catch (Exception e) {
			number = 0.0;
		}
		return df.format(number);
	}
	
	
	public static String convertPercent(BigDecimal val) {
		if(val == null) {
			return "-";
		}
		DecimalFormat df = new DecimalFormat("0.00%");
		return df.format(val);
	}
	
}
